package com.lastingwar.mall.product.service.impl;

import com.lastingwar.mall.product.dao.CategoryBrandRelationDao;
import com.lastingwar.mall.product.service.CategoryBrandRelationService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lastingwar.common.utils.PageUtils;
import com.lastingwar.common.utils.Query;

import com.lastingwar.mall.product.dao.BrandDao;
import com.lastingwar.mall.product.entity.BrandEntity;
import com.lastingwar.mall.product.service.BrandService;


@Service("brandService")
public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> implements BrandService {
    @Autowired
    CategoryBrandRelationService categoryBrandRelationService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        //接受关键字的值
        String key = (String) params.get("key");
        //添加查询条件
        QueryWrapper<BrandEntity> queryWrapper = new QueryWrapper<>();
        //如果关键字不为空，启动查询条件，搜索品牌ID的值或者模糊搜索名称
        if (StringUtils.isNotEmpty(key)){
            queryWrapper.eq("brand_id",key).or().like("name",key);
        }
        //将queryWrapper封装进page返回
        IPage<BrandEntity> page = this.page(
                new Query<BrandEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public void updateDetail(BrandEntity brand) {
        //调用之前的方法，保证自身的表先更新
        this.updateById(brand);
        //品牌名不为空保存冗余字段
        if (StringUtils.isNotEmpty(brand.getName())){
            categoryBrandRelationService.updateBrand(brand.getBrandId(),brand.getName());
            //TODO 更新其他方法
        }
    }

}